<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>我的试验</title>
    <link href="/apps/css/bootstrap.min.css" rel="stylesheet">
    <link href="/apps/css/fontawesome-free-6.4.0-web/css/all.css" rel="stylesheet">
    <link href="/apps/css/mtx2.css" rel="stylesheet">
    <script src="/apps/js/baidu-tongji-mtx.js"></script>
</head>
<body>
    <div class="container py-4">
        <div class="row mb-4">
            <div class="col">
                <h2>
                    <a href="/apps/trial" class="text-decoration-none">临床研究支持</a>
                    <i class="bi bi-chevron-right"></i>
                    我的试验
                </h2>
            </div>
        </div>

        <div class="row row-cols-1 row-cols-md-2 g-4">
            {{#each trials}}
            <div class="col">
                <div class="card h-100">
                    <div class="card-body">
                        <h5 class="card-title">
                            {{#if promotion_url}}
                            <a href="{{promotion_url}}" target="_blank" class="text-decoration-none">
                                {{trial_name}}
                                <i class="fas fa-external-link-alt ms-1 small"></i>
                            </a>
                            {{else}}
                            {{trial_name}}
                            {{/if}}
                        </h5>
                        <p class="card-text">
                            <strong>试验信息:</strong> {{formatDate start_date "YYYY-MM-DD"}}
                            <span class="badge bg-{{#if (eq trial_status 0)}}warning{{else if (eq trial_status 1)}}primary{{else if (eq trial_status 2)}}success{{else}}danger{{/if}}">{{#if (eq trial_status 0)}}准备中{{else if (eq trial_status 1)}}进行中{{else if (eq trial_status 2)}}已完成{{else}}已终止{{/if}}</span>
                            <span class="badge bg-{{#if (eq recruit_status 0)}}secondary{{else if (eq recruit_status 1)}}info{{else if (eq recruit_status 2)}}primary{{else}}dark{{/if}}">{{#if (eq recruit_status 0)}}未开放报名{{else if (eq recruit_status 1)}}已开放报名{{else if (eq recruit_status 2)}}招募进行中{{else}}招募已结束{{/if}}</span>
                        </p>
                        <p class="card-text"><strong>用户状态:</strong>
                            {{#if (eq userStatus -1)}}
                                未关注
                            {{else if (eq userStatus 0)}}
                                已关注
                            {{else if (eq userStatus 1)}}
                                已报名
                            {{else if (eq userStatus 2)}}
                                已入组
                            {{else if (eq userStatus 3)}}
                                已分组
                            {{else if (eq userStatus 4)}}
                                已退组
                            {{else}}
                                已完成
                            {{/if}}
                        </p>
                    </div>
                    <div class="card-footer">
                        <div class="d-flex flex-column gap-2">
                            <div class="btn-group">
                                {{#if (eq userStatus -1)}}
                                <div class="btn-group">
                                <button type="button" class="btn btn-sm btn-primary" onclick="followTrial({{id}})">
                                    <i class="fas fa-clipboard-list me-1"></i>关注试验
                                </button>
                                </div>
                                {{else if (eq userStatus 0)}}
                                <div class="btn-group">
                                <a href="/apps/trial/apply/{{trial_code}}" class="btn btn-sm btn-primary">
                                    <i class="fas fa-clipboard-list me-1"></i>报名参加
                                </a>
                                </div>
                                {{else if (eq userStatus 1)}}
                                <div class="btn-group">
                                <a href="/apps/trial/entry/{{trial_code}}" class="btn btn-sm btn-primary">
                                    <i class="fas fa-user-check me-1"></i>登记入组
                                </a>
                                </div>
                                {{else if (eq userStatus 2)}}
                                <div class="btn-group">
                                <a href="/apps/trial/confirm/{{trial_code}}" class="btn btn-sm btn-primary">
                                    <i class="fas fa-user-check me-1"></i>等待分组
                                </a>
                                </div>
                                {{else if (eq userStatus 3)}}
                                <div class="btn-group">
                                    {{#if ( eq trial_code 'CT-BCH-2501') }}
                                        <a href="/apps/space-journey/game" class="btn btn-sm btn-primary">
                                            <i class="fas fa-book-medical me-1"></i>在组试验</a>
                                    {{else}}
                                        <a href="/apps/trial/track/{{trial_code}}" class="btn btn-sm btn-primary">
                                            <i class="fas fa-book-medical me-1"></i>在组试验</a>
                                    {{/if}}
                                </div>
                                {{else}}
                                <div class="btn-group">
                                    <a href="#" class="btn btn-sm btn-primary">
                                        <i class="fas fa-user-check me-1"></i>查看总结
                                    </a>
                                </div>
                                {{/if}}
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            {{/each}}
        </div>
    </div>

    <!-- Footer -->
    <footer>
        <div class="text-center">
            <a id="tmall-app-home-link" href="weixin://dl/business/?appid=wx1b441d1a37b278e1&path=pages/index/index&env_version=release" class="text-decoration-none">
                <span id="tmall-app-home-title" style="color: white; font-size: 16px; margin-left: 5px;">
                    <i class="fa fa-house"></i> 广州图腾生命医学研究有限公司
                <span>
            </a>
            &copy; {{currentYear}}
        </div>
    </footer>

    <!-- 将所有JS文件移到这里，并添加defer属性 -->
    <script src="https://res.wx.qq.com/open/js/jweixin-1.6.0.js" defer></script>
    <script src="/apps/js/bootstrap.bundle.min.js" defer></script>
    <script src="/apps/js/wx-client-dom.js" defer></script>
    <script src="/apps/js/chart.js" defer></script>

    <script>
        async function followTrial(trialId) {
            try {
                const response = await fetch(`/apps/trial/my/trials/${trialId}/follow`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    }
                });

                const result = await response.json();
                if (result.success) {
                    window.location.reload();
                } else {
                    alert(result.message || '关注试验失败');
                }
            } catch (error) {
                console.error('关注试验失败:', error);
                alert('关注试验失败，请重试');
            }
        }
    </script>
</body>
</html> 
